/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Gruppe 2
 */

import java.sql.*;
import java.util.ArrayList;
import Module.*;

public class RoleController {
    /**
     * Object used to connect to the database
     */
    private Database database = new Database();
    /**
     * Method used to register a role
     * @param role
     * @return
     */
    public boolean registerRole(Role role){

        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            String[] autogen = {"id"};
            PreparedStatement statement = conn.prepareStatement(
                    "Insert into role values(DEFAULT,?)",autogen);
            statement.setString(1, role.getName());
            statement.executeUpdate();
            conn.close();
            return true;
        }catch (Exception e) {
            System.err.println(e);

        }
        return false;
    }
    /**
     * Method used to edit a role
     * @param role
     * @return
     */
    public boolean editRole(Role role){
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("Update role "
                    + "set rolename=? where roleId =?");
            statement.setString(1, role.getName());
            statement.setInt(2, role.getId());
            statement.executeUpdate();
            conn.close();
            return true;
        }catch(Exception e){
            System.err.println(e);
        }

        return false;
    }
    /**
     * Method used to delete a role
     * @param role
     * @return
     */
    public boolean deleteRole(Role role){
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("delete from role where roleid = ?");
            statement.setInt(1, role.getId());
            statement.executeUpdate();
            conn.close();
            return true;
        }catch(Exception e){
            System.err.println(e);
        }
        return false;
    }
    /**
     * Method used to get the role id to a String role
     * Used if someone want is a fasilitator and we need to save this but dosent know the id
     * @param role
     * @return
     */
    public int getRoleId(String role){
        Connection conn = null;
        int tall=0;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select * from role where rolename=?");
            statement.setString(1, role);
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                tall = rs.getInt(1);
            }
            rs.close();
            conn.close();
            return tall;

        }catch(Exception e){
            System.err.println(e);
        }
        return 0;
    }
    /**
     * Method used to get the rolename from the roleId
     * @param roleId
     * @return
     */
    public String getRoleName(int roleId){
        Connection conn = null;
        String role="";
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select * from role where roleId=?");
            statement.setInt(1, roleId);
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                role = rs.getString(2);
            }
            rs.close();
            conn.close();
            return role;

        }catch(Exception e){
            System.err.println(e);
        }
        return role;
    }
    /**
     * method used to get roles to a user
     * @param userid
     * @return
     */
    public ArrayList<String> getRoleToUser(int userid){
        Connection conn = null;
        ArrayList<String> roller = new ArrayList<String>();
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select * from usersRole where userId=?");
            statement.setInt(1,userid);
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                roller.add(getRoleName(rs.getInt(2)));
            }
            rs.close();
            conn.close();
            return roller;

        }catch(Exception e){
            System.err.println(e);
        }
        return roller;
    }
    /**
     * Method used get all roles to a user in a singel project
     * @param userID
     * @param projectId
     * @return
     */
    public ArrayList<String> getRoleToUserProject(int userID, int projectId){
        Connection conn = null;
        ArrayList<String> rols = new ArrayList<String>();
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select * from usersProject "+
            "left join role on(usersProject.roleId = role.roleId) " +
            "where userId=? and projectId=?");
            statement.setInt(1, userID);
            statement.setInt(2, projectId);
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                rols.add(rs.getString(6));
            }
            rs.close();
            conn.close();
            return rols;
        }catch(Exception e){
            System.err.println(e);
        }

        return rols;
    }
    /**
     * Method used to get roles to a session
     * @param userID
     * @param sessionId
     * @return
     */
    public ArrayList<String> getRoleToUserSession(int userID, int sessionId){
        Connection conn = null;
        ArrayList<String> rols = new ArrayList<String>();
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select * from usersSession "+
            "left join role on(usersSession.roleId = role.roleId) " +
            "where userId=? and sessionId=?");
            statement.setInt(1, userID);
            statement.setInt(2, sessionId);
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                rols.add(rs.getString(6));
            }
            rs.close();
            conn.close();
            return rols;
        }catch(Exception e){
            System.err.println(e);
        }

        return rols;
    }


    /**
     * Method used to gel all roles
     * @return
     */
    public ArrayList<Role> getAllRoles(){
        Connection conn = null;
        ArrayList<Role> roles = new ArrayList<Role>();
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select * from role");
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                roles.add(new Role(rs.getInt(1),rs.getString(2)));
            }
            conn.close();
            return roles;

        }catch(Exception e){
            System.err.println(e);
        }
        return null;
    }
}

